package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.text.JTextComponent;

import vcp.client.GUI.events.ConnectionRequestEvent;
import vcp.client.GUI.mvc.controller.UIRootController;
import vcp.client.GUI.mvc.view.custom.CustomInputVerifier;
import vcp.client.GUI.mvc.view.effects.ValidationLayerUI;
import vcp.client.interfaces.listeners.IConnectionListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.RegexStrings;
import vcp.client.utility.UtilityHelper;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;
/**
 * <p> uer interface demo screen class </p>
 * @author VCP team
 *
 */
public class UIDemoView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JButton btnLogin;
	private JButton btnOnSite;
	private JButton btnConnect;
	private JFormattedTextField ftfPort;
	private JFormattedTextField ftfHost;

	private IConnectionListener connectionLister;
	private JPanel panelConnection;

	private ValidationLayerUI hostValidationLayerUI;
	private ValidationLayerUI portValidationLayerUI;
	/**
	 * setter
	 * @param connectionLister
	 */
	public void setConnectionLister ( IConnectionListener connectionLister )
	{
		this.connectionLister = connectionLister;
	}
	/**
	 * constructor
	 */
	public UIDemoView ()
	{
		super();

		this.resetView();
	}

	/****************************************************************************************************************************
	 *** Private methods ***
	 ****************************************************************************************************************************/
	/**
	 * toggles the demo view buttons
	 * @param status
	 */
	private void toggleDemoView ( boolean status )
	{
		this.btnLogin.setEnabled(status);
		this.btnOnSite.setEnabled(status);

		UtilityHelper.getUtilityHelperInstance().setComponentsEnabled(
				this.panelConnection, ! status);
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public void updateView ()
	{
		this.toggleDemoView(true);
	}

	@Override
	public void clearView ()
	{
		this.ftfHost.setText("");
		this.ftfPort.setText("");
	}

	@Override
	public void resetView ()
	{
		this.toggleDemoView(false);
		this.btnConnect.setEnabled(false);
	}

	@Override
	protected void setupLayout ()
	{
		this.setLayout(new GridLayout(2, 1, 5, 5));
		this.hostValidationLayerUI = new ValidationLayerUI();
		this.portValidationLayerUI = new ValidationLayerUI();

		JPanel panel_6 = new JPanel();
		this.add(panel_6);
		panel_6.setLayout(new GridLayout(0, 2, 0, 0));

		this.btnLogin = new JButton(Common.BTN_LOGIN);
		panel_6.add(this.btnLogin);
		this.btnLogin.setFocusable(false);
		this.btnLogin.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.btnLogin.setName(Common.BTN_LOGIN);

		this.btnOnSite = new JButton(Common.BTN_ON_SITE);
		panel_6.add(this.btnOnSite);
		this.btnOnSite.setFocusable(false);
		this.btnOnSite.setFont(new Font("Lucida Grande", Font.PLAIN, 18));
		this.btnOnSite.setName(Common.BTN_ON_SITE);

		this.panelConnection = new JPanel();
		this.add(this.panelConnection);
		this.panelConnection.setBorder(new TitledBorder(null, "Connection", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		this.panelConnection.setPreferredSize(new Dimension(400, 312));
		this.panelConnection.setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		this.panelConnection.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new GridLayout(2, 1, 0, 0));

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 5));

		JPanel panel_4 = new JPanel();
		FlowLayout fl_panel_4 = (FlowLayout) panel_4.getLayout();
		fl_panel_4.setAlignment(FlowLayout.RIGHT);
		fl_panel_4.setVgap(8);
		panel_2.add(panel_4);

		JLabel lblHost = new JLabel("Host:");
		panel_4.add(lblHost);
		lblHost.setHorizontalAlignment(SwingConstants.CENTER);
		lblHost.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

		this.ftfHost = new JFormattedTextField();
		JLayer <JTextComponent> hostLayer = new JLayer <JTextComponent>(this.ftfHost);
		hostLayer.setUI(this.hostValidationLayerUI);
		panel_2.add(hostLayer);
		this.ftfHost.setToolTipText("Format:###.###.###.###");
		this.ftfHost.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		this.ftfHost.setPreferredSize(new Dimension(150, 35));

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new FlowLayout(FlowLayout.LEFT, 1, 5));

		JPanel panel_5 = new JPanel();
		panel_5.setPreferredSize(new Dimension(50, 30));
		FlowLayout fl_panel_5 = (FlowLayout) panel_5.getLayout();
		fl_panel_5.setAlignment(FlowLayout.RIGHT);
		panel_3.add(panel_5);

		JLabel lblPort = new JLabel("Port:");
		panel_5.add(lblPort);
		lblPort.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPort.setFont(new Font("Lucida Grande", Font.PLAIN, 15));

		this.ftfPort = new JFormattedTextField();
		JLayer <JTextComponent> portLayer = new JLayer <JTextComponent>(this.ftfPort);
		portLayer.setUI(this.portValidationLayerUI);
		panel_3.add(portLayer);
		this.ftfPort.setToolTipText("Format:#####");
		this.ftfPort.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		this.ftfPort.setPreferredSize(new Dimension(150, 35));

		JPanel panel = new JPanel();
		FlowLayout fl_panel = (FlowLayout) panel.getLayout();
		fl_panel.setAlignment(FlowLayout.RIGHT);
		this.panelConnection.add(panel, BorderLayout.SOUTH);

		this.btnConnect = new JButton(Common.BTN_CONNECT);
		panel.add(this.btnConnect);
		this.btnConnect.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.btnConnect.setRequestFocusEnabled(false);
		this.btnConnect.setFont(new Font("Lucida Grande", Font.PLAIN, 15));
		this.btnConnect.setPreferredSize(new Dimension(150, 55));
		this.btnConnect.setName(Common.BTN_CONNECT);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnOnSite.addActionListener(this);
		this.btnLogin.addActionListener(this);
		this.btnConnect.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed ( ActionEvent e )
			{
				String host = UIDemoView.this.ftfHost.getText();
				Integer port = null;

				if ( UIDemoView.this.ftfPort.getInputVerifier().verify(UIDemoView.this.ftfPort) )
				{
					port = Integer.parseInt(UIDemoView.this.ftfPort.getText());
					UIDemoView.this.connectionLister.onConnectionRequest(new ConnectionRequestEvent(this, host, port));
				}
				else
				{
					String infoMessage = String.format("Not correct %s", UIDemoView.this.ftfPort.getToolTipText());
					UIDemoView.this.controllerDelegate.showPopUpMessage(infoMessage, PopUpMessageType.INFORMATION);
				}
			}
		});

		this.ftfPort.addKeyListener(new KeyListener()
		{
			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String ftfPortText = UIDemoView.this.ftfPort.getText();
				String ftfHostText = UIDemoView.this.ftfHost.getText();

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(ftfPortText, ftfHostText);
				UIDemoView.this.btnConnect.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{

			}
		});
		this.ftfHost.addKeyListener(new KeyListener()
		{
			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String ftfPortText = UIDemoView.this.ftfPort.getText();
				String ftfHostText = UIDemoView.this.ftfHost.getText();

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(ftfPortText, ftfHostText);
				UIDemoView.this.btnConnect.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{

			}
		});

		Pattern hostPatter = UIRootController.DEBUG
				? Pattern.compile(RegexStrings.ACCEPT_ALL)
						: Pattern.compile(RegexStrings.IP);
				this.ftfHost.setInputVerifier(new CustomInputVerifier(this.hostValidationLayerUI, hostPatter));

				Pattern portPattern = UIRootController.DEBUG
						? Pattern.compile(RegexStrings.ACCEPT_ALL)
								: Pattern.compile(RegexStrings.PORT);
						this.ftfPort.setInputVerifier(new CustomInputVerifier(this.portValidationLayerUI, portPattern));
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;
		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CONNECT_TO_NETWORK, 35, 35);
		if ( icon != null )
		{
			this.btnConnect.setIcon(icon);
			this.btnConnect.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_ON_SITE, 60, 60);
		if ( icon != null )
		{
			this.btnOnSite.setIcon(icon);
			this.btnOnSite.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_LOGIN, 60, 60);
		if ( icon != null )
		{
			this.btnLogin.setIcon(icon);
			this.btnLogin.setIconTextGap(iconTextGap);
		}

	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton demoViewButton = (JButton) actionEvent.getSource();
		Screen newScreen = null;
		switch ( demoViewButton.getName() )
		{
			case Common.BTN_LOGIN:
				newScreen = Screen.LOGIN;
				break;
			case Common.BTN_ON_SITE:
				newScreen = Screen.ONSITE;
				break;
		}

		if ( newScreen != null )
		{
			this.controllerDelegate.onButtonPress(newScreen);
		}
	}
}
